/**
 * @author:cloud.lg
 * @date:2021.7.26
 * @description: 公共区域辅助函数方法
 */

import * as TYPES from "./mutation-types";
import router from '@/router/routes';

export default {
    // 保存token
    [TYPES.SAVE_TOKEN](state, now) {
        state.token = now;
    },
    [TYPES.SAVE_ISSYSTEM](state, now) {
        state.isSystem = now;
    },
    [TYPES.SAVE_ISCOLLAPSE](state, now) {
        state.isCollapse = now;
    },
    [TYPES.SAVE_EDITABLETABS](state, now) {
        if (now) {
            var setMenu = true
            state.editableTabs.forEach((v, i) => {
                if (v.menuId == now.menuId) {
                    setMenu = false
                    v.query = now.query
                    v.title = now.title
                    state.editableTabsValue = v.menuId.toString()
                    return false
                }
            })
            if (setMenu) { //没有菜单直接添加
                now.menuId = now.menuId.toString()
                state.editableTabs.push(now);
                state.editableTabsValue = now.menuId
            }
        } else {
            state.editableTabs = []
        }
    },
    // 关闭tabs导航标签
    [TYPES.DETALE_EDITABLETABS](state, now) {
        state.editableTabs.forEach((val, index) => {
            if (val.menuId === now) {
                state.editableTabs.splice(index, 1)
                if (index !== 0) {
                    if (state.editableTabsValue === now) {
                        state.editableTabsValue = state.editableTabs[index - 1].menuId
                        // 关闭后默认前往上一个
                        router.push({
                            path: state.editableTabs[index - 1].viewUri,
                            query: state.editableTabs[index - 1].query,
                        })
                    }
                } else {
                    if (state.editableTabsValue === now) {
                        // 关闭最后一个前往首页
                        state.editableTabsValue = ''
                        router.push({
                            path: '/index'
                        })
                    }
                }
            }
        })
    },
    [TYPES.SAVE_EDITABLETABSVALUE](state, now) {
        if (now) {
            state.editableTabsValue = now.toString()
        } else {
            state.editableTabsValue = ''
        }
    },
    [TYPES.SAVE_PARTNERDATA](state, now) {
        state.partnerData = now
    },
    [TYPES.SAVE_MENUDATA](state, now) {
        state.menuData = now
    },
    [TYPES.SAVE_CURRENTDATA](state, now) {
        state.currentData = now
    },
    [TYPES.SAVE_ADDRESSDATA](state, now) {
        state.addressData = now
    }
}